<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>CSS盒子模型实例</title>
    <style>
        /* 全局样式 */
        * {
            box-sizing: border-box; /* 推荐全局设置 */
            margin: 0;
            padding: 0;
        }
        
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
            display: flex;
            flex-direction: column;
            align-items: center;
            padding: 40px 20px;
        }
        
        h1 {
            color: #2c3e50;
            margin-bottom: 30px;
            text-align: center;
            text-shadow: 1px 1px 3px rgba(0,0,0,0.1);
        }
        
        .container {
            max-width: 1000px;
            width: 100%;
            display: flex;
            flex-wrap: wrap;
            gap: 40px;
            justify-content: center;
        }
        
        .box-demo {
            background: white;
            border-radius: 10px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.1);
            padding: 25px;
            width: 420px;
        }
        
        .box-title {
            color: #3498db;
            margin-bottom: 20px;
            font-size: 1.5rem;
            border-bottom: 2px solid #eee;
            padding-bottom: 10px;
        }
        
        /* 标准盒子模型 */
        .standard-box {
            box-sizing: content-box; /* 默认值 */
            width: 250px;
            height: 150px;
            background: #3498db;
            color: white;
            padding: 30px;
            border: 15px solid #2980b9;
            margin: 40px auto;
            display: flex;
            align-items: center;
            justify-content: center;
            text-align: center;
            font-weight: bold;
            position: relative;
        }
        
        /* 替代盒子模型 */
        .border-box {
            box-sizing: border-box;
            width: 250px;
            height: 150px;
            background: #e74c3c;
            color: white;
            padding: 30px;
            border: 15px solid #c0392b;
            margin: 40px auto;
            display: flex;
            align-items: center;
            justify-content: center;
            text-align: center;
            font-weight: bold;
            position: relative;
        }
        
        .dimension-label {
            position: absolute;
            background: rgba(0,0,0,0.7);
            color: white;
            padding: 3px 8px;
            border-radius: 4px;
            font-size: 12px;
            font-weight: normal;
        }
        
        .content-label { top: 0; left: 0; transform: translateY(-100%); }
        .padding-label { top: 0; right: 0; transform: translateX(100%); }
        .border-label { bottom: 0; left: 0; transform: translateY(100%); }
        .margin-label { bottom: 0; right: 0; transform: translate(50%, 50%); }
        
        .visualization {
            display: flex;
            justify-content: center;
            margin-top: 20px;
        }
        
        .size-calc {
            background: #2c3e50;
            color: white;
            padding: 15px;
            border-radius: 8px;
            margin-top: 25px;
            font-family: monospace;
        }
        
        .highlight {
            color: #f1c40f;
            font-weight: bold;
        }
        
        .note {
            background: #fff8e1;
            border-left: 4px solid #ffc107;
            padding: 15px;
            margin-top: 30px;
            border-radius: 0 8px 8px 0;
        }
    </style>
</head>
<body>
    <h1>CSS盒子模型详解</h1>
    
    <div class="container">
        <div class="box-demo">
            <h2 class="box-title">标准盒子模型 (content-box)</h2>
            <div class="standard-box">
                内容区域
                <div class="dimension-label content-label">内容: 250×150px</div>
                <div class="dimension-label padding-label">内边距: 30px</div>
                <div class="dimension-label border-label">边框: 15px</div>
                <div class="dimension-label margin-label">外边距: 40px</div>
            </div>
            
            <div class="size-calc">
                总宽度 = width(250) + padding(30×2) + border(15×2) = 
                <span class="highlight">340px</span><br>
                总高度 = height(150) + padding(30×2) + border(15×2) = 
                <span class="highlight">240px</span>
            </div>
        </div>
        
        <div class="box-demo">
            <h2 class="box-title">替代盒子模型 (border-box)</h2>
            <div class="border-box">
                内容区域
                <div class="dimension-label content-label">内容: 自动计算</div>
                <div class="dimension-label padding-label">内边距: 30px</div>
                <div class="dimension-label border-label">边框: 15px</div>
                <div class="dimension-label margin-label">外边距: 40px</div>
            </div>
            
            <div class="size-calc">
                总宽度 = width(250) =
                <span class="highlight">250px</span> (包含内容+内边距+边框)<br>
                内容宽度 = 250 - (30×2) - (15×2) =
                <span class="highlight">140px</span>
            </div>
        </div>
    </div>
    
    <div class="note">
        <h3>关键要点：</h3>
        <ul>
            <li>使用 <code>box-sizing: border-box</code> 可以更直观地控制元素尺寸</li>
            <li>外边距(margin)不参与盒子的尺寸计算，但影响元素在布局中的占用空间</li>
            <li>现代布局建议全局设置：<code>* { box-sizing: border-box; }</code></li>
            <li>内边距和边框会增加元素的总占用空间（标准模型中）</li>
        </ul>
    </div>
</body>
</html>